package br.game {
	import mx.controls.Alert;
	import mx.messaging.ChannelSet;
	import mx.messaging.channels.AMFChannel;
	import mx.rpc.AbstractOperation;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	public class Connection {
		public var server:String = "http://localhost/blade/public";
		private var gateway:String = "http://localhost/amfphp/gateway.php";
		//public var server:String = "http://www.agenciaweber.com.br/desenvolvimento/diogo";
		//private var gateway:String = "http://www.agenciaweber.com.br/desenvolvimento/diogo/amfphp/gateway.php";
		private var destination:String = "amfphp";
		private var source:String = "game.game";
		
		private var fs:Function;
		private var fe:Function;
				
		public function Connection(){
			this.fs = this.onResult;
			this.fe = this.onError;
		}
		
		public function call(service:String, command:String):Boolean {
			var remote:RemoteObject = new RemoteObject;
			var metodo:AbstractOperation = new AbstractOperation;
			var canal:AMFChannel = new AMFChannel("my-cfamf", this.gateway);
			var canalset:ChannelSet = new ChannelSet;
			
			canalset.addChannel(canal);
			
			remote.channelSet = canalset;
			remote.destination = this.destination;
			remote.source = this.source;
			
			remote.addEventListener(ResultEvent.RESULT, this.fs);
			remote.addEventListener(FaultEvent.FAULT, this.fe);			

			remote.getOperation(service).send(command);				
			
			return true;
		}
		
		
		public function setEvents(fs:Function=null, fe:Function=null):void {
			if (fs != null) {
				this.fs = fs;			
			}
			
			if(fe != null) {
				this.fe = fe;
			}
		}
		
		private function onResult(result:ResultEvent):void {
			Alert.show(result.result.toString());
		}		
		
		private function onError(fault:FaultEvent):void {
			Alert.show(fault.fault.faultString);
			//result_text.text = "code:\n" + fault.fault.faultCode + "\n\nMessage:\n" + fault.fault.faultString + "\n\nDetail:\n" + fault.fault.faultDetail;
		}		
		
	}
}